import Vue from 'vue'
import Vuex from 'vuex'
//引入微擎配置
import util from '../static/js/util.js'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		config: {},
		pageIndex: 0,
		userInfo: null
	},
	mutations: {
	
		setConfig(state, config) {
			state.config = config
		},
		setPageIndex(state, pageIndex) {
			state.pageIndex = pageIndex
		},
		setUserInfo(state, userInfo) {
			state.userInfo = userInfo
		},
	},
    getters:{
        currentColor(state){
            return state.colorList[state.colorIndex]
        }
    },
	actions: {
		
		/**
		 * 全局配置
		 */
		setConfigAsync: async function ({commit,state}) {
			return await new Promise((resolve, reject) => {
				//TODO H5端各独立模块做个判断获取自身配置项，小程序端分包方式引入无需处理，配置项获取主包配置
				let m = util.m
				//console.log(m)
				let m_config = 'muu_classroom_config'
				if(m == 'muu_classroom'){
					m_config = 'muu_classroom_config'
				}
				if(m == 'muu_minishop'){
					m_config = 'muu_minishop_config'
				}
				if(m == 'muu_scoreshop'){
					m_config = 'muu_scoreshop_config'
				}
				if(m == 'muu_activity'){
					m_config = 'muu_activity_config'
				}

				const config = util.muuCache(m_config)
				if(config){
					state.config = config;
					resolve(config)
				}else{
					getApiData()
				}
				
				function getApiData(){
					let url = '';
						//#ifdef MP-WEIXIN
						url = 'entry/wxapp/config'
						//#endif
						//#ifdef H5
						url = 'entry/site/config'
						//#endif
					util.request({
						// 小程序端执行
						url : url,
						data: {
							action : '',
							m: m
						},
						success: function (res) {
							if (res.data.code == 200) {
								// 缓存 一个小时
								util.muuCache(m_config, res.data.data, 3600)
								commit('setConfig', res.data.data)
								resolve(res.data.data)
							}
							resolve()
						}
					})
				}
			})
		},
		
		keepLogin: async function({commit, state}) {
			return new Promise((resolve, reject) => {
				console.log('执行keepLogin')
				var openid = uni.getStorageSync('openid') || "";
				if(openid != ''){
					util.request({
						url : 'entry/site/ucenter',
						data : {
							action: 'user_info',
							openid: openid,
							m: 'muu_classroom',
						},
						success: function (res) {
							
							if (res.data.code == 200) {
								let userInfo = {}
								//console.log(res.data.data,'用户数据')
								userInfo.memberInfo = res.data.data
								commit('setUserInfo', userInfo)
								uni.setStorageSync('userInfo', userInfo)
								uni.setStorageSync('openid', res.data.data.openid)
							}else{
								uni.removeStorageSync('userInfo');
							}
							resolve(res)
						}
					})
				}
				resolve()
			})
		},
		
		/**
		 * 获取已登录用户数据
		 */
		getUserInfo: async function({commit,state}){
			let that = this
			return new Promise((resolve, reject) => {
				let userInfo = uni.getStorageSync('userInfo') || {}
				if(userInfo){
					commit('setUserInfo', userInfo)
					resolve(userInfo)
				}else{
					util.getUserInfo((userInfo)=>{
						console.log('userInfo',res)
						commit('setUserInfo', userInfo)
						resolve(userInfo)
					})
				}
			})
		},
	}
})

export default store
